/**
 * @file Driver_Button.c
 * @brief 
 * @author 2bam 
 * @version 1.0
 * @date 2024-12-09
 * @copyright Copyright (c) 2024  NULL
 * 
 * @change log:
 * Date           Version Author   Describe
 * 2024-12-09     1.0     2bam     first version
 */


/* Includes -------------------------------------------------------------------*/
#include "config.h"
/* define ---------------------------------------------------------------------*/

/* typedef --------------------------------------------------------------------*/

/* variables ------------------------------------------------------------------*/
struct Button button0;
struct rt_thread button_thread;
static uint8_t button_stack[1024];
/* Private function prototypes ------------------------------------------------*/
static void ButtonCheckEntry(void *para);
static void ReadENCValue(void);



/**
 * @brief 按钮单击事件
 * @param  btn         My Param doc
 * @date 2024-12-09
 * @author 2bam 
 */
void Callback_SINGLE_CLICK_Handler(void* btn)
{
    //OLED_PrintASCIIString(0,32,"Single Click.",&afont8x6,OLED_COLOR_NORMAL);
    //OLED_ShowFrame();

    OLED_MsgQueSend(msg_click);


}



/**
 * @brief 
 * @param  btn         My Param doc
 * @date 2024-12-09
 * @author 2bam 
 */
void Callback_DOUBLE_CLICK_Handler(void* btn)
{
    //OLED_PrintASCIIString(0,32,"Double Click.",&afont8x6,OLED_COLOR_NORMAL);
    //OLED_ShowFrame();

    OLED_MsgQueSend(msg_return);
}

/**
 * @brief 
 * @return uint8_t 
 * @date 2024-12-09
 * @author 2bam 
 */
uint8_t read_button0_GPIO(void)
{
    return (uint8_t)HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin);
}

uint8_t read_button1_GPIO(void)
{
    return (uint8_t)HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
}


/**
 * @brief 
 * @date 2024-12-09
 * @author 2bam 
 */
int buttons_register(void)
{
    rt_err_t ret;

    button_init(&button0, read_button0_GPIO, 0);
    button_attach(&button0, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
    button_attach(&button0, DOUBLE_CLICK, Callback_DOUBLE_CLICK_Handler);
    button_start(&button0);

    ret = rt_thread_init(&button_thread,
                        "button thread",
                        ButtonCheckEntry, NULL,
                        button_stack, sizeof(button_stack),
                        RT_THREAD_PRIORITY_BUTTON, 5);
    rt_thread_startup(&button_thread);
    return ret;
}
INIT_APP_EXPORT(buttons_register);

/**
 * @brief 
 * @param  para        My Param doc
 * @date 2024-12-15
 * @author 2bam 
 */
static void ButtonCheckEntry(void *para)
{
    HAL_TIM_Encoder_Start(&htim8,TIM_CHANNEL_ALL);
    /** 先将定时器计数器清零 2025-01-01 Wednesday 16:44:59 */
    __HAL_TIM_SET_COUNTER(&htim8,32768);

    while (1)
    {
        button_ticks();
        ReadENCValue();
        rt_thread_mdelay(TICKS_INTERVAL);
    }
}


static void ReadENCValue(void)
{
    static int32_t count = 32768;
    static int32_t last_cnt = 32768;
   
    count = __HAL_TIM_GET_COUNTER(&htim8);

    if(count-last_cnt>1)
    {
        rt_kprintf("lastcnt:%d\n",last_cnt);
        rt_kprintf("cnt:%d\n",count);
        last_cnt = count;
        OLED_MsgQueSend(msg_down);
        rt_kprintf("msg down\n");
    }
    else if(last_cnt-count>1)
    {
        rt_kprintf("lastcnt:%d\n",last_cnt);
        rt_kprintf("cnt:%d\n",count);
        last_cnt = count;
        OLED_MsgQueSend(msg_up);
        rt_kprintf("msg up\n");
    }
}
